package club.monkeywood.controller.buyer;

import club.monkeywood.dataobject.Buyer;
import club.monkeywood.dataobject.OrderDetail;
import club.monkeywood.dataobject.OrderMaster;
import club.monkeywood.dataobject.Production;
import club.monkeywood.enums.ResponseEnum;
import club.monkeywood.enums.order.OrderStatusEnum;
import club.monkeywood.enums.order.PayStatusEnum;
import club.monkeywood.exception.SellException;
import club.monkeywood.request.buyer.OrderRequest;
import club.monkeywood.request.buyer.SelectedProductionRequest;
import club.monkeywood.service.buyer.*;
import club.monkeywood.util.key.KeyUtil;
import club.monkeywood.vo.buyer.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;


@Api(tags = "点餐系统微信买家端后台接口")  //文档说明
@Validated   //验证参数
@RestController
@RequestMapping("/")
@Slf4j
public class BuyerController {

    @Autowired
    private ProductionCategoryService productionCategoryService;

    @Autowired
    private ProductionInfoService productionInfoService;

    @Autowired
    private RatingService ratingService;

    @Autowired
    private SellerService sellerService;

    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private BuyerService buyerService;

    @GetMapping("/hello")
    public String hello() {
        return "hello buyer";
    }

    @GetMapping("/getproductions")
    public ServerResponseVO getProductions(@RequestParam("id") Integer sellerId) {

        List<ProductionCategoryVO> productionCategoryVOList = productionCategoryService.getProductions(sellerId);
        return ServerResponseVO.createBySuccess(productionCategoryVOList);

    }

    @GetMapping("/getseller")
    public ServerResponseVO getSeller(@RequestParam("id") Integer id) {

        SellerVO sellerVO = sellerService.getSeller(id);

        return ServerResponseVO.createBySuccess(sellerVO);

    }

    @GetMapping("/getratings")
    public ServerResponseVO getRatings(@RequestParam("id") Integer id) {

        List<RatingVO> ratingVOList = ratingService.queryRatingBySellerId(id);
        return ServerResponseVO.createBySuccess(ratingVOList);
    }

    @ApiOperation("创建订单")
    @PostMapping("/createorder")
    public ServerResponseVO createOrder(@Validated @RequestBody OrderRequest orderRequest,
                                        BindingResult bindingResult) {

        log.info("进入【创建订单】BuyerController.createOrder");
        log.info("【创建订单】请求参数orderRequest:");
        log.info("{}", orderRequest);

        //1.参数取值范围冗错
        if (bindingResult.hasErrors()) {
            log.error("【创建订单】参数不正确，orderRequest={}", orderRequest);
            throw new SellException(ResponseEnum.PARAM_ERROR.getCode()
                    , bindingResult.getFieldError().getDefaultMessage());
        }

        //2.订单入库，扣库存
        OrderMasterVO orderMasterVO = orderMasterService.createOrder(orderRequest);

        return ServerResponseVO.createBySuccess(orderMasterVO);
    }

    @ApiOperation("查询用户信息")
    @PostMapping("/getuser")
    public ServerResponseVO createOrder(@RequestParam("openid") String openid) {
        Buyer buyer = buyerService.findByOpenid(openid);
        return ServerResponseVO.createBySuccess(buyer);
    }

}
